import 'package:flutter/material.dart';
import 'package:lianwenke20220712/bean/news_list.dart';
import 'package:lianwenke20220712/http/httpUtils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 15:19
///
/// @Description 文字新闻页面
class TextNewsWidget extends StatefulWidget {
  const TextNewsWidget({Key key}) : super(key: key);

  @override
  _TextNewsWidgetState createState() => _TextNewsWidgetState();
}

class _TextNewsWidgetState extends State<TextNewsWidget> with AutomaticKeepAliveClientMixin {
  int page =1,size = 5;
  RefreshController _refreshController;
  List<Result> _list = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _refreshController = RefreshController();
    NewsBean();
  }
  //避免内存泄漏
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _refreshController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SmartRefresher(
        controller: _refreshController,
        enablePullUp: true,
        enablePullDown: true,
        onRefresh: (){
          page =1;
          NewsBean();
          _refreshController.refreshCompleted();
        },
        onLoading: (){
          page++;
          NewsBean();
          _refreshController.loadComplete();
        },
        // ignore: missing_return
        child: _list == null ? Center(child: CircularProgressIndicator(),) : ListView.builder(itemBuilder: (BuildContext context, int index){
            if(_list[index].whetherAdvertising == 2){
              return Container(
                height: 150,
                margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15),
                    color: Colors.white
                ),
                child:Center(child: Text(_list[index].infoAdvertisingVo.content,style: TextStyle(color: Colors.black),)),
              );
            }else{
              return Container(
            height: 150,
            margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              color: Colors.white
            ),
            child:Center(child: Text(_list[index].title,style: TextStyle(color: Colors.black),)),
          );
            }
        },itemCount: _list.length,),
      ),
    );
  }

  void NewsBean() {
    HttpUtils().MendGet('http://mobile.bwstudent.com/hotNews/news/v1/findColumnNewsList',isCache: true,map: {'columnId':1001,'page':page,'size':size},onSuccess: (data){
      var result = NewsList.fromJson(data).result;
      if(page == 1){
        _list.clear();
      }
      if(result.length == null || result.length < size){
        _refreshController.loadNoData();
      }else{
        _refreshController.resetNoData();
      }

      setState(() {
        _list.addAll(result);
      });
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
